package com.chaoshi.strategy.impl;

import com.chaoshi.strategy.PriceStrategy;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class FullReductionDecorator implements PriceStrategy {
    private final PriceStrategy decorated;
    private final int threshold;
    private final int amount;

    public FullReductionDecorator(PriceStrategy decorated,
                                  int threshold, int amount) {
        this.decorated = decorated;
        this.threshold = threshold;
        this.amount = amount;
    }

    @Override
    public BigDecimal calculate(int a, int s, int m) {
        BigDecimal total = decorated.calculate(a, s, m);
        // 计算满减次数（向下取整）
        BigDecimal thresholdBD = new BigDecimal(threshold);
        int times = total.divide(thresholdBD, 0, RoundingMode.DOWN).intValue();

        // 计算减去的总金额
        BigDecimal discount = new BigDecimal(amount).multiply(new BigDecimal(times));
        return total.subtract(discount).setScale(2, RoundingMode.HALF_UP);
    }
}
